package com.foo.example.action;

import java.io.IOException;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foo.common.base.action.FooGenericAction;
import com.foo.common.base.utils.FooUtils;
import com.foo.example.model.MyExample;
import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import com.google.gson.Gson;

@ExceptionMappings({ @ExceptionMapping(exception = "java.lang.Exception",
		result = "unhandledException") })
@Results({ @Result(name = "unhandledException",
		location = "/WEB-INF/public/jsps/unhandledException.jsp") })
@InterceptorRefs({ @InterceptorRef("exampleAuthInterceptor"),
		@InterceptorRef("defaultStack") })
@ParentPackage("example")
public class ExampleAction extends FooGenericAction {

	private static final long serialVersionUID = 3609540005224931735L;
	Logger logger = LoggerFactory.getLogger(ExampleAction.class);

	/**
	 * 返回json字符串
	 */
	@Action("/example/testForCrud")
	public void testForCrud() {

		logger.info("testForCrud");

		MyExample myExample = new MyExample();
		myExample.setExpStr("中文字段测试");
		logger.info("saving...");
		fooGenericService.save(myExample);

		myExample.setExpInt(100);
		logger.info("updating...");
		fooGenericService.update(myExample);

		logger.info("deleting...");
		fooGenericService.delete(myExample);

		FooUtils.printSuccessJson(response, myExample);
	}

	/**
	 * 返回json字符串
	 */
	@Action("/example/testForJson")
	public void testForJson() {
		FooUtils.printSuccessJson(response);
	}

	/**
	 * 返回jsp页面
	 */
	@Action(value = "/example/testForDispatcher", results = { @Result(
			name = "success",
			location = "/WEB-INF/example/jsps/testForDispatcher.jsp",
			type = "dispatcher") })
	public String testForDispatcher() {
		logger.info("testForDispatcher");
		return "success";
	}

	/**
	 * 调用特殊拦截器的方法
	 */
	@Action(value = "/example/testForInterceptor",
			interceptorRefs = { @InterceptorRef("specialInterceptor") })
	public void testForInterceptor() {
		logger.info("testForInterceptor");
		FooUtils.printSuccessJson(response);
	}

	/**
	 * 测试异常
	 */
	@Action(value = "/example/testForException")
	public void testForException() {
		logger.info("testForException");
		throw new RuntimeException("testForException");
	}

	@Action(value = "/example/helloWorld")
	public void helloWorld() throws IOException {
		Map<String, Object> myResultMap = Maps.newHashMap();
		myResultMap.put("ret", 0);
		myResultMap.put("msg", "");
		Gson gson = new Gson();
		response.setCharacterEncoding(Charsets.UTF_8.toString());
		response.setContentType("text/plain");
		response.getWriter().write(gson.toJson(myResultMap));
	}

}
